package cn.edu.nhic.nhcarsb.controller;

import cn.edu.nhic.nhcarsb.entity.News;
import cn.edu.nhic.nhcarsb.entity.R;
import cn.edu.nhic.nhcarsb.service.NewsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.javassist.runtime.Desc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/news")
public class NewsController extends BaseController{
    @Autowired
    private NewsService newsService;

    @GetMapping("/{nid}")//news/1
    public R show(@PathVariable int nid) {
        News news = newsService.getById(nid);
        System.out.println("hello world");
        if (news != null) {
            return R.success(news);
        } else {
            return R.error("新闻不存在！");
        }
    }
    /*
    R:
    code: 200:success ; 500:fail
    data: 数据
    msg: 错误信息
    成功：返回数据
    失败：返回错误信息
    */

    @GetMapping("/{pno}/{psize}")//news/1/10
    public R list(@PathVariable int pno, @PathVariable int psize) throws InterruptedException {
        Page<News> page = new Page<>(pno, psize);
        //SELECT nid,ntitle,ndesc,ndate FROM news where nid>3 order by nid desc
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();//自定义查询条件
        queryWrapper.select("nid", "ntitle", "ndesc", "ndate");//select nid,ntitle,ndesc,ndate
        queryWrapper.gt("nid", 3);//nid>3
        queryWrapper.orderByDesc("nid");//order by nid desc
        Page<News> newsPage = newsService.page(page, queryWrapper);//查询
//        Thread.sleep(5000);//模拟延迟，5秒
        return R.success(newsPage);
    }
}
